<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小红心</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        #app {
            width: 100%;
            height: 100%;
        }

        .container {
            position: relative;
            width: 100%;
            height: 100%;
        }

        .img {
            position: absolute;
            width: 100px;

        }
    </style>

</head>

<body>
    <div id="app">
        <div class="container" v-on:click="containerClick">
            <img src="heartImg.jpeg" alt="" v-for="item in list" class="img"
                v-bind:style="{left:item.left+'px',top:item.top+'px',opacity:item.opacity}">
        </div>
    </div>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                show: false,
                list: [],
                timer: null,
            },
            methods: {
                containerClick(e) {
                    // 1.获取鼠标点击的坐标位置
                    let left = e.x;
                    let top = e.y;
                    console.log(left,top);
                    this.list.push({
                        left: left-50,
                        top: top-50,
                        speed: .5,
                        opacity:1,
                    });
                    // 2.开启定时器进行遍历
                    this.timer = setInterval(() => {
                        for (let i = 0; i < this.list.length; i++) {
                            this.list[i].opacity-=0.005;
                            this.list[i].top -= this.list[i].speed;
                            if (this.list[i].opacity < 0) {
                                this.list.splice(i,1)
                                this.show = false;
                                clearInterval(this.timer);
                                this.timer=null
                            }
                        }

                    }, 16)


                },



            }

        });
    </script>
</body>

</html>